package com.ctshk.sapp.visa.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.finance.dto.onaccount.QueryQuotaDTO;
import com.ctshk.rpc.finance.req.onaccount.QueryQuotaReq;
import com.ctshk.rpc.finance.service.IFinanceOnAccountCurrencyQuotaService;
import com.ctshk.rpc.system.dto.MainDataPaymentRefundTypeDTO;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.req.MainDataPaymentRefundTypeListReq;
import com.ctshk.rpc.system.service.IMainDataPaymentRefundTypeService;
import com.ctshk.rpc.system.service.ISysUserService;
import com.ctshk.rpc.visa.dto.visa.VisaOrderInfoResp;
import com.ctshk.rpc.visa.dto.visa.VisaOrderListDTO;
import com.ctshk.rpc.visa.req.visa.*;
import com.ctshk.rpc.visa.service.IVisaOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * <p>
 * 签证订单 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-28
 */
@Api(value="订单中心签证", tags="订单中心签证")
@RestController
@RequestMapping("/visa-order")
public class VisaOrderController extends BaseController {
    @DubboReference
    private IVisaOrderService iVisaOrderService;

    @DubboReference
    private IMainDataPaymentRefundTypeService mainDataPaymentRefundTypeService;

    @DubboReference
    private ISysUserService sysUserService;

    @DubboReference
    private IFinanceOnAccountCurrencyQuotaService onAccountCurrencyQuotaService;

    @ApiOperation(value="查看收款退款方式列表", notes="查看收款退款方式列表(黄利斌)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "签证列表", operDesc = "签证列表")
    @PostMapping("/gathering")
    public Result<PageResponse<MainDataPaymentRefundTypeDTO>> gathering(@RequestBody @Valid MainDataPaymentRefundTypeListReq req,@RequestParam(required = true) String currencyId){
        // 查询当前账户对应部门
        UserDTO user = sysUserService.queryCacheUser(getCurrentUser().getId());
        if (user == null){
            return Result.failed(SystemError.USER_1002);
        }
        PageResponse<MainDataPaymentRefundTypeDTO> list = mainDataPaymentRefundTypeService.list(req);
        QueryQuotaReq queryQuotaReq = new QueryQuotaReq();
        queryQuotaReq.setAccountType(2);
        queryQuotaReq.setDeptId(user.getDeptId());
        queryQuotaReq.setCurrencyId(Long.parseLong(currencyId));
        QueryQuotaDTO data = onAccountCurrencyQuotaService.queryQuota(queryQuotaReq).getData();
        // 是否有挂账额度判断
        if (data != null) {
            int compare = data.getAvailableAmount().compareTo(BigDecimal.ZERO);
            if (compare ==0) {
                if (list.getRecords() != null && list.getRecords().size() > 0) {
                    list.setRecords(list.getRecords().stream().filter(r -> !r.getId().equals(1372449671248781313L)).collect(Collectors.toList()));
                }
            }
        }
        return Result.success(list);
    }

    @ApiOperation(value="订单中心签证列表", notes="签证列表(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "签证列表", operDesc = "签证列表")
    @PostMapping("/list")
    public Result<PageResponse<VisaOrderListDTO>> visaList(@RequestBody VisaOrderListReq visaOrderListReq) {
        PageResponse<VisaOrderListDTO> list = iVisaOrderService.list(visaOrderListReq);
        return Result.success(list);
    }

    @ApiOperation(value="订单中心签证详情", notes="订单中心签证详情(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "签证详情", operDesc = "签证详情")
    @PostMapping("/info")
    public Result<VisaOrderInfoResp> visaInfo(@RequestBody @Valid VisaOrderInfoReq visaOrderInfoReq) {
        return iVisaOrderService.query(visaOrderInfoReq);
    }

    @ApiOperation(value="保存签证申请", notes="保存签证申请(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "保存签证申请", operDesc = "保存签证申请")
    @PostMapping("/save-visa")
    public Result saveVisa(@RequestBody @Valid VisaHandleReq visaHandleReq) {
        TokenUser user = getCurrentUser(getRequest());
        return Result.success(iVisaOrderService.add(visaHandleReq,user));
    }

    @ApiOperation(value="订单操作收款", notes="订单操作收款(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "订单操作收款", operDesc = "订单操作收款")
    @PostMapping("/collection-visa")
    public Result collection(@RequestBody @Valid Visa0rderCollectionDetailReq rderCollectionDetailReq) {
        return Result.success(iVisaOrderService.visaOrderCollection(rderCollectionDetailReq,getCurrentUser(getRequest())));
    }

    @ApiOperation(value="待付款状态取消订单", notes="待付款状态取消订单(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "取消订单", operDesc = "取消订单")
    @PostMapping("/cancel-visa-order")
    public Result<Long> cancelVisaOrder(@RequestBody @Valid Visa0rderCancelReq visa0rderCancelReq) {
        return iVisaOrderService.cancelVisaOrder(visa0rderCancelReq,getCurrentUser(getRequest()));
    }

    @ApiOperation(value="待确认、确认成功、确认失败状态取消订单", notes="待确认、确认成功、确认失败状态取消订单(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "取消订单", operDesc = "取消订单")
    @PostMapping("/cancel-refund-order")
    public Result<Long> cancelVisaOrderRefund(@RequestBody @Valid Visa0rderRefundDetailReq visa0rderRefundDetailReq) {
        return iVisaOrderService.cancelVisaOrderRefund(visa0rderRefundDetailReq,getCurrentUser(getRequest()));
    }

    @ApiOperation(value="通知旅客", notes="通知旅客(陈雷)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "通知旅客", operDesc = "通知旅客")
    @PostMapping("/cancel-notice-tourist")
    public Result cancelVisaOrderRefund(@RequestBody @Valid Visa0rderNoticeReq visa0rderNoticeReq) {
        return Result.success(iVisaOrderService.notice(visa0rderNoticeReq));
    }

    @ApiOperation(value="退款审核", notes="退款审核(黄利斌)")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.UPDATE, operTitle = "退款审核", operDesc = "退款审核")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid VisaOrderRefundApprovalReq visaOrderRefundApprovalReq){
        visaOrderRefundApprovalReq.setUserId(getCurrentUser().getId());
        return iVisaOrderService.approval(visaOrderRefundApprovalReq);
    }
}
